//
//  WDCalendarMonthView.m
//  Demos
//
//  Created by iMac on 2018/3/2.
//  Copyright © 2018年 iMac. All rights reserved.
//

#import "WDCalendarMonthView.h"
#import "WDCalendarDateHandler.h"
#import "WDCalendarDayModel.h"
#import "WDCalendarViewCell.h"
#import "WDCalendarDayModel.h"


static NSString *cellID = @"WDCalendarCellID";
@interface WDCalendarMonthView() <UICollectionViewDataSource, UICollectionViewDelegate>



@end

@implementation WDCalendarMonthView




- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame collectionViewLayout:[self getFlowLayout]];
    if (self) {
        self.delegate = self;
        self.dataSource = self;
        [self setupInit];
        self.backgroundColor = [UIColor yellowColor];
    }
    return self;
}

#pragma mark - UICollectionView DataSource-delegate

- (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    WDCalendarViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    cell.model = self.datas[indexPath.item];
    
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    // NSLog(@"numberOfItemsInSection: %ld", arr.count);
    NSLog(@"%ld", self.datas.count);
    return self.datas.count;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (void)setupInit {
    [self registerClass:[WDCalendarViewCell class] forCellWithReuseIdentifier:cellID];
    self.backgroundColor = [UIColor lightGrayColor];
    self.bounces = NO;
    self.showsVerticalScrollIndicator = NO;
    self.showsHorizontalScrollIndicator = NO;
    [self reloadData];
    
}
- (void)setDatas:(NSArray *)datas {
    _datas = datas;
    [self reloadData];
}

- (UICollectionViewFlowLayout *)getFlowLayout {
    CGFloat itemWidth = [self getItemWidth];
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(itemWidth, itemWidth);
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    return flowLayout;
}
- (CGFloat)getItemWidth {
    CGFloat width = SCREEN_WIDTH;
    CGFloat itemWidth = width / 7.f;
    return itemWidth;
}
- (void)changeFrame:(NSDate *)date {
    CGFloat width = SCREEN_WIDTH;
    CGFloat itemWidth = width / 7.f;
    CGFloat height = itemWidth * [[WDCalendarDateHandler sharedInstance] numsOfRows:date];
    if (self.monthSizeChanged) {
        self.monthSizeChanged(CGSizeMake(width, height));
    }
}


@end
